﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using ProgNetComponents.ExtenderProviders;
using ProgNetComponents.ExtenderProviders.Settings;

namespace ProgNetComponents.Design
{
    public class FakeSite : ISite
    {
        private IComponent _Component;
        private IContainer _Container;
        private IServiceProvider _ServiceProvider;
        private BaseExtenderProvider _Owner;

        public FakeSite(IComponent component, IContainer container, IServiceProvider serviceProvider)
        {
            _Component = component;
            _Container = container;
            _ServiceProvider = serviceProvider;
            _Name = GetUniqueName();
        }
        public FakeSite(IComponent component, BaseExtenderProvider owner)
        {
            _Component = component;
            _Owner = owner;
            _Name = GetUniqueName();
        }

        #region ISite Members

        public IComponent Component
        {
            get { return _Component; }
        }

        public IContainer Container
        {
            get
            {
                if (_Container != null)
                    return _Container;
                if (_Owner != null && _Owner.Site != null)
                    return _Owner.Site.Container;
                return null;
            }
        }

        public bool DesignMode
        {
            get { return true; }
        }
        
        private string _Name;

        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }

        private string GetUniqueName()
        {
            string _Name = Component.GetType()+"0";
            if (Component != null)
            {
                if (_Container != null)
                {
                    INameCreationService s = GetService(typeof(INameCreationService)) as INameCreationService;
                    if (s != null)
                    {
                        _Name = s.CreateName(_Container, Component.GetType());
                    }
                }
                else if (_Owner != null)
                {
                    IEnumerable<BaseExtenderSettings> en = _Owner.GetAllSettings();
                    _Name = Component.GetType().Name;
                    int idx = 1;
                    bool unique = false;
                    while (!unique)
                    {
                        unique = true;
                        foreach (BaseExtenderSettings s in en)
                        {
                            if (s.Site == null)
                                return _Name;
                            if (s.Site.Name == _Name + idx)
                            {
                                unique = false;
                                idx++;
                                break;
                            }
                        }
                    }
                    _Name += idx;
                }
            }
            return _Name;
        }
        #endregion

        #region IServiceProvider Members

        public object GetService(Type serviceType)
        {
            if (_ServiceProvider != null)
                return _ServiceProvider.GetService(serviceType);
            if (_Owner != null && _Owner.Site != null)
                return _Owner.Site.GetService(serviceType);
            return null;
        }

        #endregion

    }
}
